package com.chenfei.datasource.config;

import com.github.pagehelper.PageHelper;
import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.mybatis.spring.boot.autoconfigure.MybatisProperties;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

@Configuration
@AutoConfigureAfter({DataSourceConfiguration.class})
@MapperScan(basePackages="com.zb.jjz.prodshelf.mapper")
public class MybatisConfiguration extends MybatisAutoConfiguration implements TransactionManagementConfigurer{
	
	public MybatisConfiguration(MybatisProperties properties, ObjectProvider<Interceptor[]> interceptorsProvider,
                                ResourceLoader resourceLoader, ObjectProvider<DatabaseIdProvider> databaseIdProvider) {
		super(properties, interceptorsProvider, resourceLoader, databaseIdProvider);
		System.out.println("********+++++++++=======************************=====================++++++++++++++++++++++++++");
	}

	@Autowired
	PageHelper pageHelper;

	@Resource(name = "masterDataSource")
	private DataSource masterDataSource;
  
	@Resource(name = "slaveDataSource")
	private DataSource slaveDataSource;
	
	@Resource(name = "dataSource")
    private DataSource dataSource;

	@Override
	@Bean(name="sqlSessionFactory")
  	public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource) throws Exception {
		
		 SqlSessionFactoryBean fb = new SqlSessionFactoryBean();
		// 指定数据源(这个必须有，否则报错)
		 fb.setDataSource(dataSource);
		 fb.setPlugins(new Interceptor[]{pageHelper});
		 // 下边两句仅仅用于*.xml文件，如果整个持久层操作不需要使用到xml文件的话（只用注解就可以搞定），则不加
		 //fb.setTypeAliasesPackage(env.getProperty("mybatis.typeAliasesPackage"));// 指定基包
		 fb.setConfigLocation(new PathMatchingResourcePatternResolver().getResource("classpath:mybatis-config.xml"));
		 fb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*.xml"));
		 return fb.getObject();
	}
 
	@Bean(name="dataSource")
	public ReadWriteSplitRoutingDataSource dataSource(){
		ReadWriteSplitRoutingDataSource proxy = new ReadWriteSplitRoutingDataSource();
		Map<Object,Object> targetDataResources = new HashMap<Object, Object>();
		targetDataResources.put(DataBaseContextHolder.DataBaseType.MASTER.getCode(), masterDataSource);
		targetDataResources.put(DataBaseContextHolder.DataBaseType.SLAVE.getCode(), slaveDataSource);
		//默认源
    	proxy.setDefaultTargetDataSource(masterDataSource);
    	proxy.setTargetDataSources(targetDataResources);
    	return proxy;
	}

	@Override
	public PlatformTransactionManager annotationDrivenTransactionManager() {
		return new DataSourceTransactionManager(dataSource);
	}

}
